package com.qf.service.impl;

import com.qf.dao.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public BaseResult login(HttpServletRequest req, User user) {
        User user1 = userMapper.findById(user.getUid());
        BaseResult baseResult = new BaseResult();
        if (user == null) {
            baseResult.setCode(0);
            baseResult.setMessage("用户名不存在");
            return baseResult;
        }
        if (!user1.getUserPass().equals(user.getUserPass())) {
            baseResult.setCode(0);
            baseResult.setMessage("密码输入错误");
            return baseResult;
        }
        baseResult.setMessage("登录成功");
        req.getSession().setAttribute("user", user);
        return baseResult;
    }

    @Override
    public BaseResult register(HttpServletRequest req, User user) {
        BaseResult baseResult = new BaseResult();
        User user1 = userMapper.findById(user.getUid());
        if (user1!=null){
            baseResult.setCode(0);
            baseResult.setMessage("用户名已存在");
            return baseResult;
        }
        userMapper.add(user);
        baseResult.setCode(1);
        return baseResult;
    }
}
